package com.ruoyi.web.controller.leftover;


import com.alibaba.fastjson.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.*;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.system.service.ICompanyService;
import com.ruoyi.system.service.ILeftoverOutService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 边角料出庫控制层
 *
 * @author C3007106
 */
@Controller
@RequestMapping("/system/leftoverOut")
public class LeftoverOutController extends BaseController {

    private String prefix = "leftover/out";

    @Autowired
    private ILeftoverOutService leftoverOutService;

    @Autowired
    private ICompanyService companyService;

    @GetMapping("/view")
    public String view() {
        return prefix + "/view";
    }

    @GetMapping("/stock")
    public String stock() {
        return prefix + "/stock";
    }

    @GetMapping("/confirm")
    public String confirm() {
        return prefix + "/confirm/view";
    }

    /**
     * 出庫審核
     */
    @RequiresPermissions("system:leftoverOut:confirm")
    @Log(title = "出庫審核", businessType = BusinessType.UPDATE)
    @PostMapping("/confirm")
    @ResponseBody
    public AjaxResult confirm(@Validated LeftoverOut leftoverOut) {
        return toAjax(leftoverOutService.updateLeftoverOutState(leftoverOut));
    }

    @RequiresPermissions("system:leftoverOut:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LeftoverOutDetail leftoverOutDetail) {
        startPage();
        List<LeftoverOutDetail> list = leftoverOutService.selectLeftoverOutList(leftoverOutDetail);
        return getDataTable(list);
    }

    /**
     * 修改
     */
    @RequiresPermissions("system:leftoverOut:edit")
    @GetMapping("/edit/{tableId}")
    public String edit(@PathVariable("tableId") String tableId, ModelMap mmap) {
        mmap.put("leftoverOut", leftoverOutService.selectLeftoverOutByTableId(tableId));
        return prefix + "/edit";
    }

    /**
     * 修改保存
     */
    @RequiresPermissions("system:leftoverOut:edit")
    @Log(title = "出庫管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated LeftoverOutDetail leftoverOutDetail) {
        return toAjax(leftoverOutService.updateLeftoverOutDetail(leftoverOutDetail));
    }

    /**
     * 新增出庫
     */
    @RequiresPermissions("system:leftoverOut:add")
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        mmap.put("companies", companyService.selectCompanyList(new Company()));
        return prefix + "/add";
    }

    /**
     * 新增保存出庫
     */
    @RequiresPermissions("system:leftoverOut:add")
    @Log(title = "出庫管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("file") MultipartFile file, LeftoverOutMessage leftoverOutMessage) throws IOException {

        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        leftoverOutMessage.setAppendix(fileName);

        LeftoverOutDetail[] leftoverOutDetails = JSON.parseObject(leftoverOutMessage.getLeftoverOutDetail(), LeftoverOutDetail[].class);
        return toAjax(leftoverOutService.insertLeftoverOut(leftoverOutMessage, leftoverOutDetails));
    }
}
